/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc;

import midpmpc.config.Config;
import javax.microedition.midlet.*;

import javax.microedition.io.*;
import java.io.*;
import java.util.*;

import org.j4me.logging.Log;

import org.j4me.ui.Menu;
import org.j4me.ui.MenuItem;

import org.j4me.ui.UIManager;


/**
 * @author vvs
 */
public class Mpc extends MIDlet {

    private boolean midletPaused = false;
    StreamConnection mpdConn = null;
    InputStream stream = null;
    TimerTask refreshTask=new TimerTask()
    {
        public void run() {
            updateViews();
        }
    };
    Timer updTimer;
    MpdConnection m;
    NowPlayingView npv;
    PlaybackMenu pbm;
    PlaylistView plv;
    LibraryView lv;
    ServerListView slv;
    Config conf;
    Menu main,playback;
    CallbackMenuItem play,pause,next,prev,stop;
    CallbackMenuItem exit,disconnect;
    static Hashtable mpdInfo;
    class CallbackMenuItem implements MenuItem
    {
        Mpc lis;
        String _text;
        public boolean direct;
        public CallbackMenuItem(String text,Mpc listener,boolean isDirectCmd)
        {
            lis=listener;
            _text=text;
            direct=isDirectCmd;
        }
        public CallbackMenuItem(String text,Mpc listener)
        {
            lis=listener;
            _text=text;
            direct=false;
        }
        public String getText()
        {
            return _text;
        }
        public void onSelection()
        {
            //Log.warn("callback");
            lis.onMenuSelection(this);
        }
    }

    /**
     * The Mpc constructor.
     */
    public Mpc() {
        mpdInfo=new Hashtable();
        npv=new NowPlayingView(this);
        //npv.setCommandListener(this);
        pbm=new PlaybackMenu();
        plv=new PlaylistView(this);
        lv=new LibraryView(this);
        conf=new Config();
        conf.read();
        slv=new ServerListView(this);
        main=new Menu();
        main.appendMenuOption(npv);
        playback=new Menu("Playback control",main);
        play=new CallbackMenuItem("play", this,true);
        pause=new CallbackMenuItem("pause", this,true);
        stop=new CallbackMenuItem("stop", this,true);
        next=new CallbackMenuItem("next", this,true);
        prev=new CallbackMenuItem("prev", this,true);
        playback.appendMenuOption(play);
        playback.appendMenuOption(pause);
        playback.appendMenuOption(stop);
        playback.appendMenuOption(next);
        playback.appendMenuOption(prev);
        main.appendSubmenu(playback);
        exit=new CallbackMenuItem("Exit", this);
        main.appendMenuOption(plv);
        main.appendMenuOption(lv);
        main.appendMenuOption(exit);
    }
    void onMenuSelection(CallbackMenuItem item)
    {
        Log.warn("menu selection");
        if (item==exit)
        {
            destroyApp(true);
            notifyDestroyed();
        }
//            exitMIDlet();
        else if (item.direct)
        {
            m.sendCmd(item.getText());
        }
    }

    void start()
    {
        UIManager.init(this);
        slv.show();
    }
    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {

        if (midletPaused) {
        } else {
            start();
        }
        conf.read();
        Log.warn("starting...");
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
    public void connect(final String addr)
    {
        final Mpc t=this;
        new Thread(){
        public void run(){
            try
            {
            m=new MpdConnection(t);
            String ret=m.connect(addr);
            updTimer = new Timer();
            updateViews();
            main.show();
            updTimer.scheduleAtFixedRate(refreshTask, 0, 5000);
            }
            catch (IOException e)
            {
                Log.error("connect()",e);
            }
            catch (NullPointerException e)
            {
                Log.error("connect()",e);
            }
        }}.start();
//        alert.setString("works");


    }
    public void updateViews()
    {
        updateStatus();
        npv.update();
        plv.update();

    }
    public static void updateStatus()
    {
        mpdInfo.clear();
        MpdResponse sdata = MpdConnection.sendCmd("status");
        sdata.copy(mpdInfo);
        sdata = MpdConnection.sendCmd("currentsong");
        sdata.copy(mpdInfo);

    }
}
